package com.ja.openchat.ui.message;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.AppViewModelFactory;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.zy.openchat.core.model.config.AppConfigInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.databinding.FragmentAddContactBinding;
import com.ja.openchat.viewmodels.AddContactFragmentViewModel;
import com.ja.openchat.views.CustomEditText;

import me.goldze.mvvmhabit.base.BaseFragment;

public class AddContactFragment extends BaseFragment<FragmentAddContactBinding, AddContactFragmentViewModel> {


    private int positon;

    public static AddContactFragment newInstance(int position){
        AddContactFragment fragment = new AddContactFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position",position);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_add_contact;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initView() {
        super.initView();
        viewModel.setActivity(getActivity());
        viewModel.my_user_value.set(getString(R.string.my_username_text)+"："+UserInfo.getUserInfo().username);
        if(getArguments()!=null){
            positon = getArguments().getInt("position");
        }
        if(positon == 0){
            AppConfigInfo configInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();
            if(configInfo!=null&&!configInfo.can_see_qr_code){
                binding.myUserValue.setCompoundDrawablesRelative(null,null,null,null);
                binding.myUserValue.setClickable(false);
                binding.myUserValue.setEnabled(false);
            }
            binding.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取系统剪贴板
                    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
                    ClipData clipData = ClipData.newPlainText(null, viewModel.userNameSub.get());
                    // 把数据集设置（复制）到剪贴板
                    clipboard.setPrimaryClip(clipData);
                    ToastUtils.showShort(getResources().getString(R.string.copy_successful_text));
                }
            });
        }else{
            binding.llNum.setVisibility(View.GONE);
            binding.etSearch.setHint(getString(R.string.input_group_name));
            binding.myUserValue.setVisibility(View.GONE);
            binding.scanBtn.setVisibility(View.GONE);
            binding.addContactBtn.setVisibility(View.GONE);
        }

        binding.etSearch.setOnXTextChangeListener(new CustomEditText.OnXTextChangeListener() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(positon==0){
                    if(TextUtils.isEmpty(s.toString())){
                        binding.scanBtn.setVisibility(View.VISIBLE);
                        binding.addContactBtn.setVisibility(View.VISIBLE);
                        binding.recyclerView.setVisibility(View.GONE);
                    }else{
                        binding.scanBtn.setVisibility(View.GONE);
                        binding.addContactBtn.setVisibility(View.GONE);
                        binding.recyclerView.setVisibility(View.VISIBLE);
                    }
                }
                viewModel.doSearch(s.toString(),positon);
            }
        });
        viewModel.initList(binding.recyclerView);
    }

    @Override
    public AddContactFragmentViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(AddContactFragmentViewModel.class);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getActivity().startActivityForResult( data,requestCode);
    }
}
